94. Binary Tree Inorder Traversal

Posted wangyufeiaichiyu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了94. Binary Tree Inorder Traversal相关的知识,希望对你有一定的参考价值。

题目链接:https://leetcode.com/problems/binary-tree-inorder-traversal/

 

解题思路:

二叉树的中序遍历。

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode 
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x)  val = x; 
 8  * 
 9  */
10 class Solution 
11     public List<Integer> inorderTraversal(TreeNode root) 
12         
13         Stack<TreeNode> stack = new Stack<>();
14         
15         List<Integer> res= new ArrayList<>();
16         if(root==null)
17             return res;
18         
19         TreeNode pnode = root;
20         while(pnode!=null || !stack.isEmpty())
21         
22             if(pnode!=null)
23             
24                 stack.push(pnode);
25                 pnode = pnode.left;
26             
27             else
28             
29                 pnode = stack.pop();
30                 res.add(pnode.val);
31                 pnode = pnode.right;
32             
33         
34         return res;
35     
36 

 

以上是关于94. Binary Tree Inorder Traversal的主要内容,如果未能解决你的问题,请参考以下文章

94. Binary Tree Inorder Traversal

#Leetcode# 94. Binary Tree Inorder Traversal

(Tree)94.Binary Tree Inorder Traversal

94. Binary Tree Inorder Traversal

94. Binary Tree Inorder Traversal

94. Binary Tree Inorder Traversal