Skip to content

Instantly share code, notes, and snippets.

@esase
Created April 6, 2022 05:05
Show Gist options
  • Save esase/9cdfdcc5798e2031e06652c862b9cf1a to your computer and use it in GitHub Desktop.
Save esase/9cdfdcc5798e2031e06652c862b9cf1a to your computer and use it in GitHub Desktop.
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number[]}
*/
var inorderTraversal = function(root) {
const result = [];
getNodeValues(root, result);
return result;
};
var getNodeValues = function(node, result) {
if (!node) {
return;
}
if (node.left) {
getNodeValues(node.left, result);
}
result.push(node.val);
if (node.right) {
getNodeValues(node.right, result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment