题目:

二叉树的中序遍历

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

样例

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

   1
    \
     2
    /
   3

返回 [1,3,2].

挑战

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

解题:

程序直接来源

Java程序:

/**
 * 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.
     */
    public ArrayList<Integer> inorderTraversal(TreeNode root) {
        // write your code here
        ArrayList<TreeNode> p = new ArrayList<TreeNode>();  
        ArrayList<Integer> res = new ArrayList<Integer>();  
        while(root != null || p.size() != 0){  
            while(root != null){  
                p.add(root);  
                root = root.left;  
            }  
            root = p.get(p.size()-1);  
            p.remove(p.size()-1);  
            res.add(root.val);  
            root = root.right;  
        }  
        return res;  
    }
    
}
View Code

总耗时: 1238 ms

Python程序:

"""
Definition of TreeNode:
class TreeNode:
    def __init__(self, val):
        self.val = val
        self.left, self.right = None, None
"""


class Solution:
    """
    @param root: The root of binary tree.
    @return: Inorder in ArrayList which contains node values.
    """
    def inorderTraversal(self, root):
        # write your code here
        p = [root]  
        res = [0]  
        while root is not None or len(p) != 1:  
            while root is not None:  
                p.append(root)  
                root = root.left  
            root = p[len(p)-1]  
            del p[len(p)-1]  
            res.append(root.val)  
            root = root.right  
        n = len(res)  
        return res[1:n]
View Code

总耗时: 263 ms

非递归程序,理解不透,还需要人丑就要多读书

根据上面灵感,递归程序如下:

java程序:

/**
 * 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.
     */
    public ArrayList<Integer> inorderTraversal(TreeNode root) {
        // write your code here
        
        ArrayList<Integer> res = new ArrayList<Integer>();  
        res = inorderTrun(res,root);
        return res;  
    }
    public ArrayList<Integer> inorderTrun(ArrayList<Integer> res,TreeNode root){
        if(root == null)
            return res;
        if(root!=null){
            if(root.left!=null){
                res = inorderTrun(res,root.left);
            }
            res.add(root.val);
            if(root.right!=null){
                res = inorderTrun(res,root.right);
            }
        }
        return res;
    }
    
}
View Code

总耗时: 1714 ms

Python程序:

"""
Definition of TreeNode:
class TreeNode:
    def __init__(self, val):
        self.val = val
        self.left, self.right = None, None
"""


class Solution:
    """
    @param root: The root of binary tree.
    @return: Inorder in ArrayList which contains node values.
    """
    def inorderTraversal(self, root):
        # write your code here
        res = []
        res = self.inorderTrun(res,root)
        return res

    def inorderTrun(self,res,root):
        if root==None:
            return res
        if root.left!=None:
            res = self.inorderTrun(res,root.left)
        res.append(root.val)
        if root.right!=None:
            res = self.inorderTrun(res,root.right)
        return res
View Code

总耗时: 213 ms

 根据上面的程序理解,可根据栈实现,上面定义的ArrayList也是起到栈的作用