Leetcode No.94
趁昨天的二叉树继续写一道
JS
/** * 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 a =root =>{
if(!root){
return 0;
}else{
a(root.left);
res.push(root.val);
a(root.right);
};
};
a(root);
return res;
};
Python
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def inorderTraversal(self, root: TreeNode) -> List[int]:
arr=[]
def a(self,root):
if not root:
return;
a(self,root.left)
arr.append(root.val)
a(self,root.right)
a(self,root)
return arr
还没有评论,来说两句吧...