/**
* 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> preorderTraversal(TreeNode root) {
List<Integer> result = new LinkedList<Integer>();
if(root!=null)
preOrder(root, result);
return result;
}
public static void preOrder(TreeNode node, List<Integer> list)
{
list.add(node.val);
if (node.left!=null) {
preOrder(node.left, list);
}
if (node.right!=null) {
preOrder(node.right, list);
}
}
}
144. Binary Tree Preorder Traversal
最新推荐文章于 2021-07-03 15:32:49 发布