二叉树中序遍历——94. Binary Tree Inorder Traversal

来源:互联网 发布:Js委托和代理的区别 编辑:程序博客网 时间:2024/05/17 07:41

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].

/** * Definition for a binary tree node. * function TreeNode(val) { *     this.val = val; *     this.left = this.right = null; * } *//** * @param {TreeNode} root * @return {number[]} */var inorderTraversal = function(root) {    var stack = [],        node,        result = [];    if (!root) return result;    node = root;    while (node || stack.length) {        while (node) {            stack.push(node);            node = node.left;        }        node = stack.pop();        result.push(node.val);        node = node.right;    }    return result;};
阅读全文
0 0
原创粉丝点击