94. Binary Tree Inorder Traversal 二叉树中序遍历
Given a binary tree, return the inorder traversal of its nodes' values.
For example:
Given binary tree [1,null,2,3],
1
\
2
/
3
return [1,3,2].
Note: 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;* }*//*** @param {TreeNode} root* @return {number[]}*/let inorderTraversal = (root) => {let res = [];if (root == null) return res;let stack = [];var node = root;while (node != null || stack.length != 0) {while (node != null) {stack.push(node);node = node.left;}if (stack.length != 0) {node = stack.pop();res.push(node.val)node = node.right;}}return res;}

浙公网安备 33010602011771号