注意以下几点
1. 输入为null时,返回空list,题目没有说
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public List<Integer> preorderTraversal(TreeNode root) { if(root == null){ return new ArrayList<Integer>(); } List<Integer> result = new ArrayList<Integer>(); result.add(root.val); if(root.left!=null){ result.addAll(preorderTraversal(root.left)); } if(root.right != null){ result.addAll(preorderTraversal(root.right)); } return result; } }
相关推荐
javascript js_leetcode题解之144-binary-tree-preorder-traversal.js
144.Binary_Tree_Preorder_Traversal二叉树的前序遍历【LeetCode单题讲解系列】
python python_leetcode题解之144_Binary_Tree_Preorder_Traversal
144-Binary Tree Preorder Traversal94-Binary Tree Inorder Traversal145-Binary Tree Postorder Traversal(后续的非递归时间不够可以先跳过,有点难)层次遍历,队列辅助,相当于广搜。102-Binary Tree Level ...
105.construct-binary-tree-from-preorder-and-inorder-traversal (从前序与中序遍历序列构造二叉树) 106.construct-binary-tree-from-inorder-and-postorder-traversal (从中序与后序遍历序列构造二叉树) 112.path-...
lru缓存leetcode LeetCode_Note leetcode 个人笔记 问题清单 [1_two_sum.cpp] [10_regular-expression-matching.cpp] [100_same-tree.cpp] ...[107_binary-tree-level-order-traversal-ii.cpp] [108_convert-sorted
binary-tree-preorder-traversal 链表 linked-list-cycle-ii 链表 linked-list-cycle 链表 copy-list-with-random-pointer 复杂度 single-number 动态规划 candy 贪心 gas-station 动态规划 palindrome-partitioning...
LeetCodeLeetCode solutions(Java)树Minimum Depth of Binary Tree栈evaluate-reverse-polish-notation穷举max-points-on-a-line链表sort-list排序insertion-sort-list树binary-tree-postorder-traversal树binary-...
105.construct_binary_tree_from_preorder_and_inorder_traversal从前序
construct-binary-tree-from-preorder-and-inorder-traversal 无官方题解 106 construct-binary-tree-from-inorder-and-postorder-traversal 无官方题解 116 populating-next-right-pointers-in-eac
* [Binary Search Tree](https://github.com/kamyu104/LeetCode#binary-search-tree) * [Breadth-First Search](https://github.com/kamyu104/LeetCode#breadth-first-search) * [Depth-First Search]...
lru cache leetcode leetcode 记录自己刷leetcode时遇到的一些值得记下来的...binary-tree-preorder-traversal n-queens-ii populating-next-right-pointers-in-each-node sum-root-to-leaf-numbers best-time-to-buy
Leetcode问题和Java访谈问题 10个正则表达式Matching.js 100个相同的Tree.js 101对称Tree.js 102 Binary Tree Level Order Traversal.js(二叉树级订单Traversal.js) 103 Binary Tree Zigzag Level Order ...
leetcode 530 力扣在线评委 # 问题 困难 解决方案 1 简单的 2 中等的 3 中等的 12 中等的 22 中等的 39 中等的 94 中等的 108 中等的 122 中等的 136 中等的 144 中等的 167 中等的 238 中等的 260 中等的 268 中等...
Javascript 的解决方案Leetcode Problems and interview problems in Javascript10 Regular Expresion Matching.js100 Same Tree.js101 Symmetric Tree.js102 Binary Tree Level Order Traversal.js103 Binary Tree ...
LeetCode笔记本docsifyjsLeetCode算法Java c / c ++ javascript的基本知识简单的1. Two Sum 704. Classical Binary Search2. 3 Sum206. Reverse Linked List中等的... Binary Tree Preorder Traversal145. Binary Tree
Leetcode扑克 项目简介 该项目为《剑指Offer》题解 OnlineJudge 题目 个人建议能使用LeetCode还是尽量用LeetCode。因为LeetCode题目接口更为规范,而且测试数据也更为全面。 牛客网 LeetCode 二维数组中的查找 240. ...
java lru leetcode ...Tree/BinaryTree BST HashTable Disjoint Set Trie BloomFilter LRU Cache Algorithm General Coding InOrder/PreOrder/PostOrder Traversal Greedy Recursion/Backtrace Breadth-fi
2sum leetcode 轮廓 1_count_and_say.cpp - super_ugly_number.cpp ...invert_Binary_Tree.cpp - 对称树.cpp - BST_Or_Not.cpp - level_order_traversal.cpp - exponentiation_by_squaring.cpp - Maximum_Depth_B