题目:二叉树的中序遍历

来源:互联网 发布:房租占收入比例 知乎 编辑:程序博客网 时间:2024/04/26 01:33

给出一棵二叉树,返回其中序遍历

您在真实的面试中是否遇到过这个题?
Yes
哪家公司问你的这个题?AirbnbAlibaba Amazon Apple Baidu Bloomberg Cisco Dropbox Ebay Facebook Google Hulu Intel Linkedin Microsoft NetEase Nvidia Oracle Pinterest Snapchat Tencent Twitter Uber Xiaomi Yahoo Yelp Zenefits
感谢您的反馈
样例

给出二叉树 {1,#,2,3},

   1    \     2    /   3

返回 [1,3,2].

挑战

你能使用非递归算法来实现么?

标签 Expand
递归二叉树 二叉树遍历



相关题目 Expand         

/**
* Definition of TreeNode:
* public class TreeNode {
*     public int val;
*     public TreeNode left, right;
*     public TreeNode(int val) {
*         this.val = val;
*         this.left = this.right = null;
*     }
* }
*/

public class Solution {
    /**
     * @param root: The root of binary tree.
     * @return: Inorder in ArrayList which contains node values.
     */
    private  ArrayList<Integer> result = new ArrayList<>();
    public ArrayList<Integer> inorderTraversal(TreeNode root) {
        // write your code here
        if(root==null) return result;
         if(root.left!=null){
              inorderTraversal(root.left);
         }
         result.add(root.val);
         if(root.right!=null){
              inorderTraversal(root.right);
         }
         return result;
    }
}



0 0
原创粉丝点击