给出一棵二叉树,返回其中序遍历
您在真实的面试中是否遇到过这个题?给出二叉树 {1,#,2,3}
,
1 \ 2 / 3
返回 [1,3,2]
.
你能使用非递归算法来实现么?
相关题目 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;
}
}