Leetcode 102 Binary Tree Level Order Traversal
Description:
Given a binary tree, return the level order traversal of its nodes’ values. (ie, from left to right, level by level).
For example:
Given binary tree [3,9,20,null,null,15,7]
,
|
|
return its level order traversal as:
|
|
Methods:
Use bfs to go through the whole tree, and use a queue to get the node.
Problem is: how to know the tree level.
We can use a variable count to know this. When count ==0, which means we have accomplish a level of the tree. Then the size of queue would be the number of nodes in the next level.
Everytime we pop a node ,we minus count.
Code:
|
|
Leetcode 538 Conver BST to Greater Tree
Description:
Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST.
|
|
Methods:
Go through the tree from the node which has the largest value to the one that has the least value.(The reverse version of in-order traversal).
Code:
|
|
Leetcode 79 Word Search
Description:
Given a 2D board and a word, find if the word exists in the grid.
The word can be constructed from letters of sequentially adjacent cell, where “adjacent” cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.
For example,
Given board =
|
|
word = "ABCCED"
, -> returns true
,
word = "SEE"
, -> returns true
,
word = "ABCB"
, -> returns false
.
Methods:
Use dfs
Code:
|
|
Leetcode 212 Word Search II
Description:
Given a 2D board and a list of words from the dictionary, find all words in the board.
Each word must be constructed from letters of sequentially adjacent cell, where “adjacent” cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once in a word.
For example,
Given words = ["oath","pea","eat","rain"]
and board =
|
|
Return
|
|
Methods:
Use Word Search
Code:
|
|
But there is better way..