从零开始Leetcode – Day 12
Construct Binary Tree from Inorder and Postorder Traversal
https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/
递归时有点复杂, 明天把把11,12的一起干了。 明天开始进入angular.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| var buildTree = function(inorder, postorder) { node_nums = inorder.length; post_i = node_nums - 1; return build_tree(0, node_nums-1); function build_tree(start, end) { if (start <= end) { var root = new TreeNode(postorder[post_i]); var root_index = inorder.indexOf(postorder[post_i]) post_i--; root.right = build_tree(root_index+1, end); root.left = build_tree(start, root_index-1); return root; } return null; } };
|