/**
* 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) {
let ret = []
function dfs(root) {
if(root === null) {
return root
}
dfs(root.left)
ret.push(root.val)
dfs(root.right)
}
dfs(root)
return ret
};
var inorderTraversal = function (root) {
let res = []
if (!root) return res
let stack = []
while (stack.length || root) {
while (root) {
stack.push(root)
root = root.left
}
root = stack.pop()
res.push(root.val)
root = root.right
}
return res
};
/**
* 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 res = []
const rec = (root) => {
if(!root) return
rec(root.left)
res.push(root.val)
rec(root.right)
}
rec(root)
return res
};
原创文章,作者:czhdawn,如若转载,请注明出处:https://www.czhdawn.cn/archives/4922
