标签:while turn when track first put class ber null
Given a binary tree, return the inorder traversal of its nodes‘ values.
Example:
Input: [1,null,2,3] 1 2 / 3 Output: [1,3,2]Follow up: Recursive solution is trivial, could you do it iteratively?
/** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } */ function getLefts(root) { if (!root) { return []; } let result = []; let current = root; while(current) { result.push(current); current = current.left ; } return result; } /** * @param {TreeNode} root * @return {number[]} */ var inorderTraversal = function(root) { let lefts = getLefts(root); let result = []; while (lefts.length) { let newRoot = lefts.pop(); result.push(newRoot.val); if (newRoot.right) { let newLefts = getLefts(newRoot.right); lefts = lefts.concat(newLefts) } } return result; };
The idea is
[Algorithm] 94. Binary Tree Inorder Traversal iteratively approach
标签:while turn when track first put class ber null
原文地址:https://www.cnblogs.com/Answer1215/p/11992069.html