Binary Tree Inorder Traversa

?

package cn.edu.xidian.sselab.hashtable;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 *
 * @author zhiyong wang
 * title: Binary Tree Inorder Traversal
 * content:
 * Given a binary tree, return the inorder(中序) traversal of its nodes‘ values.
 *
 * For example:
 * Given binary tree {1,#,2,3},
 *
 *    1
 *     \
 *      2
 *     /
 *    3
 *
 * return [1,3,2]
 *
 */
public class BinaryTreeInorderTraversal {

//with stack to solve this question
    public List<Integer> inorderTraversal(TreeNode root){
        TreeNode node = root;
        Stack<TreeNode> stack = new Stack<TreeNode>();
        List<Integer> list = new ArrayList<Integer>();
        while(node != null || !stack.isEmpty()){
            while(node != null){
                stack.push(node);
                node = node.left;
            }
            if(!stack.isEmpty()){
                node = stack.pop();
                list.add(node.val);
                node = node.right;
            }
        }
        return list;
    }
    
    //with recursive to solve this question
    //这个地方注意三个地方:(1)if(node == null)的时候,返回的是list,而不是null
    //                   (2) 题目中定义的是left与right,而不是leftNode,rightNode
    //                    (3)递归返回的结果要加到list中,所以用list.addAll();
    public List<Integer> inorderTraversals(TreeNode root){
        TreeNode node = root;
        List<Integer> list = new ArrayList<Integer>();
        if(node == null) return list;
        list.addAll(inorderTraversals(node.left));
        list.add(node.val);
        list.addAll(inorderTraversals(node.right)) ;
        return list;
    }
    
}

class TreeNode{
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode(int val){
        this.val = val;
    }
}

时间: 2024-10-14 09:12:18

Binary Tree Inorder Traversa的相关文章

LeetCode Binary Tree Inorder Traversal

LeetCode解题之Binary Tree Inorder Traversal 原题 不用递归来实现树的中序遍历. 注意点: 无 例子: 输入: {1,#,2,3} 1 2 / 3 输出: [1,3,2] 解题思路 通过栈来实现,从根节点开始,不断寻找左节点,并把这些节点依次压入栈内,只有在该节点没有左节点或者它的左子树都已经遍历完成后,它才会从栈内弹出,这时候访问该节点,并它的右节点当做新的根节点一样不断遍历. AC源码 # Definition for a binary tree node

leetcode -day29 Binary Tree Inorder Traversal &amp; Restore IP Addresses

1.  Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? 分析:求二叉树的中序

41: Binary Tree Inorder Traversal

/************************************************************************/            /*       41:  Binary Tree Inorder Traversal                               */            /************************************************************************/  

Binary Tree Inorder Traversal

Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? 先贴递归实现的吧 1 /** 2

LeetCode: Binary Tree Inorder Traversal 解题报告

Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3},   1    \     2    /   3return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? co

[LeetCode][JavaScript]Binary Tree Inorder Traversal

Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? https://leetcode.

【LeetCode】Binary Tree Inorder Traversal (2 solutions)

Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? 解法一:递归 /** * Defi

LeetCode:Binary Tree Inorder Traversal

题目:Binary Tree Inorder Traversal 二叉树的中序遍历,和前序.中序一样的处理方式,代码见下: 1 struct TreeNode { 2 int val; 3 TreeNode* left; 4 TreeNode* right; 5 TreeNode(int x): val(x), left(NULL),right(NULL) {} 6 }; 7 8 vector<int> preorderTraversal(TreeNode *root) //非递归的中序遍历(

[Leetcode][JAVA] Binary Tree Preorder Traversal, Binary Tree Inorder Traversal, Binary Tree Postorder Traversal

Binary Tree PreOrder Traversal: Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3   return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? 不使用递归前序遍历,可以