Created
April 6, 2022 05:05
-
-
Save esase/9cdfdcc5798e2031e06652c862b9cf1a to your computer and use it in GitHub Desktop.
Binary tree inorder traversal [https://leetcode.com/problems/binary-tree-inorder-traversal/]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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