94. Binary Tree Inorder Traversal

时间:2023-03-09 06:56:45
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].

代码如下:

 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list=new ArrayList<>(); if(root==null)
return list; if(root.left!=null)
list.addAll(inorderTraversal(root.left));
list.add(root.val);
if(root.right!=null)
list.addAll(inorderTraversal(root.right)); return list;
}
}