
LeetCode BinaryTree+BSF+DFS
文章平均质量分 53
Spencer_Lin
If you fight for your dream, one day....
展开
-
Binary Tree Zigzag Level Order Traversal Java
/*Key to solve: BFS Trough Tree level by level(BFS) 1.treeCurr List: store TreeNode of current level 2.curr List: store the value of TreeNode of current level into curr List 3.Use a I原创 2014-08-01 09:58:47 · 434 阅读 · 0 评论 -
Unique Binary Search Trees Java
Given n, how many structurally unique BST's (binary search trees) that store values 1...n?For example,Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1 \原创 2014-08-27 13:26:45 · 450 阅读 · 0 评论 -
Unique Binary Search Trees II Java
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n.For example,Given n = 3, your program should return all 5 unique BST's shown below. 1 3原创 2014-08-27 16:30:11 · 550 阅读 · 0 评论 -
Binary Tree Inorder Traversal LeetCode Java
import java.util.Stack;import java.util.List;import java.util.ArrayList;//inorder : left -> middle -> right//using stack to track nodes//Time Complexity: O(n)//Space Complexity: O(n)public clas原创 2014-07-11 08:25:28 · 350 阅读 · 0 评论 -
Binary Tree Level Order Traversal LeetCode Java
Key to solve: BFS by ArrayList Create a TreeNode list that including all the nodes for each level, terminate loop if list is empty Build another Integer list that including value of each lev原创 2014-09-21 09:26:01 · 403 阅读 · 0 评论 -
Validate Binary Search Tree LeetCode Java
Normal Approach: In_Order Approach Idea: simply do In_Order traversal, then check If In_Order traversal of a binary tree is sorted, then the binary tree is BST. else notpublic static原创 2014-09-21 10:54:39 · 553 阅读 · 0 评论 -
Path Sum II Java
/* Key to solve: DFS, Recursion, BackTracking similar with previous problem of Path Sum, instead of print out all path that meet equal to user inout "sum" 1.Run DFS 2.Backtracking原创 2014-07-30 16:19:53 · 383 阅读 · 0 评论 -
Sum Root to Leaf Numbers Java
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ /* Key to solve: recursion,原创 2014-07-30 15:03:57 · 296 阅读 · 0 评论 -
Binary Tree Postorder Traversal Java
/*Given a binary tree, return the postorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [3,2,1].Note: Recursive solution is trivia原创 2014-07-11 08:57:22 · 388 阅读 · 0 评论 -
Binary Tree Preorder Traversal Java
/*Given a binary tree, return the preorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,2,3].Note: Recursive solution is trivial原创 2014-07-11 09:15:25 · 426 阅读 · 0 评论 -
Word Ladder Java
Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that:Only one letter can be changed at a timeEach intermediate word m原创 2014-08-19 15:29:45 · 413 阅读 · 0 评论 -
Binary Tree Maximum Path Sum Java
/* Key to Solve: * Recursion, * array as a reference object to store max sum The max path sum is base on 4 cases possibility: 1. root-self. 2. L-subSum + R-subSum + root 3. L-su原创 2014-07-30 15:03:51 · 361 阅读 · 0 评论 -
Convert Sorted List to Binary Search Tree Java
/* Key to solve: DFS 1.find length of LinkedList 2. Need to set the mid point of Linkedlist as root in order to construct Binary Search Tree 3.recursively construct left sub tree from原创 2014-08-01 16:12:43 · 449 阅读 · 0 评论 -
Construct Binary Tree from Preorder and Inorder Traversal Java
Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.原创 2014-08-19 16:58:51 · 383 阅读 · 0 评论 -
Minimum Depth of Binary Tree Java
/* Solution1: key to solve: DFS, recursion 1. */ public static int minDepthDFS(TreeNode root) { //base case if (root == null) return 0; //recursion原创 2014-07-31 15:59:44 · 343 阅读 · 0 评论 -
Construct Binary Tree from Inorder and Postorder Traversal Java
Given inorder and postorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.原创 2014-08-19 17:01:15 · 381 阅读 · 0 评论