94. Binary Tree Inorder Traversal

Posted 烁宝宝

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了94. 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].

代码如下:

 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 public class Solution {
11     public List<Integer> inorderTraversal(TreeNode root) {
12         List<Integer> list=new ArrayList<>();
13         
14         if(root==null)
15         return list;
16         
17         if(root.left!=null)
18         list.addAll(inorderTraversal(root.left));
19         list.add(root.val);
20         if(root.right!=null)
21         list.addAll(inorderTraversal(root.right));
22         
23         return list;
24     }
25 }

 

以上是关于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