- 浏览: 19301 次
- 性别:
- 来自: 北京
文章分类
最新评论
Given a binary tree, return the preorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3},
1
\
2
/
3
return [1,2,3].
思路:二叉树的前序遍历
For example:
Given binary tree {1,#,2,3},
1
\
2
/
3
return [1,2,3].
思路:二叉树的前序遍历
package leetcode; import java.util.ArrayList; import java.util.List; public class BinaryTreePreorderTraversal { public List<Integer> preorderTraversal(TreeNode root) { List<Integer> list = new ArrayList<Integer>(); if(root == null) return list; list.add(root.val); if(root.left != null) list.addAll(preorderTraversal(root.left)); if(root.right != null) list.addAll(preorderTraversal(root.right)); return list; } }
发表评论
-
Java中String与StringBuffer的区别
2014-10-29 21:07 302String和StringBuffer的区别,网上资料可以说 ... -
String to Integer (atoi)
2014-10-29 17:13 401题目描述 Implement atoi to convert ... -
Implement strStr()
2014-10-28 15:17 289题目描述 Implement strStr(). Retu ... -
Valid Palindrome
2014-10-23 10:32 425题目描述 Given a string, determine ... -
ZigZag Conversion
2014-10-22 19:51 346题目描述 The string "PAYPALIS ... -
Add Binary
2014-10-22 19:43 305题目描述 Given two binary strings, ... -
Longest Common Prefix
2014-10-22 19:44 333题目描述 Write a function to find t ... -
Count and Say
2014-10-22 19:44 348题目描述 The count-and-say sequence ... -
Valid Sudoku
2014-10-21 10:22 358题目描述 Determine if a Sudoku is v ... -
Valid Parentheses
2014-10-21 09:41 327题目描述 Given a string containing ... -
Palindrome Number
2014-10-21 09:41 349题目描述 Determine whether an integ ... -
Length of Last Word
2014-10-21 09:41 359题目描述 Given a string s consists ... -
Minimum Depth of Binary Tree
2014-10-21 09:41 310题目描述 Given a binary tree, find ... -
Remove Nth Node From End of List
2014-10-20 16:36 259题目描述 Given a linked list, remov ... -
Path Sum
2014-10-20 15:37 299题目描述 Given a binary tree and a ... -
Binary Tree Level Order Traversal II
2014-10-20 11:17 236题目描述 Given a binary tree, retur ... -
Binary Tree Level Order Traversal
2014-10-20 11:03 293题目描述 Given a binary tree, retur ... -
Pascal's Triangle II
2014-10-20 10:07 262题目描述 Given an index k, return t ... -
Pascal's Triangle
2014-10-19 12:24 322题目描述 Given numRows, generate th ... -
Plus One
2014-10-19 11:51 340题目描述 Given a non-negative numbe ...
相关推荐
144.Binary_Tree_Preorder_Traversal二叉树的前序遍历【LeetCode单题讲解系列】
Construct Binary Tree from Preorder and Inorder Traversal 根据先序,中序建立二叉树
144-Binary Tree Preorder Traversal94-Binary Tree Inorder Traversal145-Binary Tree Postorder Traversal(后续的非递归时间不够可以先跳过,有点难)层次遍历,队列辅助,相当于广搜。102-Binary Tree Level ...
Construct Binary Tree from Preorder and Inorder Traversal Construct Binary Tree from Inorder and Postorder Traversal 二叉查找树 Unique Binary Search Trees Unique Binary Search Trees II Validate Binary...
105.construct_binary_tree_from_preorder_and_inorder_traversal从前序
leetcode 530 力扣在线评委 # 问题 困难 解决方案 1 简单的 2 中等的 3 中等的 ...Binary Tree Preorder Traversal 2016.06.06 94. Binary Tree Inorder Traversal 318. Maximum Product of Word Length
* [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]...
LeetCode笔记本docsifyjsLeetCode算法Java c / c ++ javascript的基本知识简单的1. Two Sum 704. Classical Binary Search2. 3 Sum206. Reverse Linked List中等的... Binary Tree Preorder Traversal145. Binary Tree
102 Binary Tree Level Order Traversal.js(二叉树级订单Traversal.js) 103 Binary Tree Zigzag Level Order Traversal.js(二叉树之字形级别顺序Traversal.js) 104 Binary Tree.js的最大深度 105从Preorder和...
class BinaryTree: def __init__(self, root_value): self.root = TreeNode(root_value) # 前序遍历 def preorder_traversal(self, node): if node: print(node.val, end=" ") # 访问根节点 self.preorder_...
reverse-polish-notation穷举max-points-on-a-line链表sort-list排序insertion-sort-list树binary-tree-postorder-traversal树binary-tree-preorder-traversal链表reorder-list链表linked-list-cycle-ii链表linked-...
binary-tree-preorder-traversal 链表 linked-list-cycle-ii 链表 linked-list-cycle 链表 copy-list-with-random-pointer 复杂度 single-number 动态规划 candy 贪心 gas-station 动态规划 palindrome-partitioning...
[105_construct-binary-tree-from-preorder-and-inorder-traversal.cpp] [106_construct-binary-tree-from-inorder-and-postorder-traversal.cpp] [107_binary-tree-level-order-traversal-ii.cpp] [108_convert-...
数据结构二叉树功能展示包括以完全前序序列创建二叉树"; cout<<" create in preorder and in... cout<<" if complete binary tree ... 判断是否是一颗二叉树"; cout打印该树"; cout回收该树"; cout退出操作";
Tree From Preorder Traversal Hard (73.62 %) [1027] Longest Arithmetic Sequence Medium (41.26 %) [1026] Maximum Difference Between Node and Ancestor Medium (55.48 %) [1025] Divisor Game Easy (58.58 %) ...
105.construct-binary-tree-from-preorder-and-inorder-traversal (从前序与中序遍历序列构造二叉树) 106.construct-binary-tree-from-inorder-and-postorder-traversal (从中序与后序遍历序列构造二叉树) 112.path-...
2. Depth First Traversal(Pre-order,Post-order,In-order) 您可以在此处了解有关这些算法的更多信息: 运行项目 1. Clone the repo. 2. Install dependencies using `npm install` or `yarn install`. 3. Run the ...
construct-binary-tree-from-preorder-and-inorder-traversal 无官方题解 106 construct-binary-tree-from-inorder-and-postorder-traversal 无官方题解 116 populating-next-right-pointers-in-eac
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
Binary Tree from Preorder and Inorder Traversal 用两个栈实现队列 232. Implement Queue using Stacks 旋转数组的最小数字 153. Find Minimum in Rotated Sorted Array 斐波那契数列 509. Fibonacci Number 跳...