Journey of LeetCode|DAY 13
Preface
This is a new day to continue my binary tree journey.
Learn something new and keep reviewing what I learnt before.
1. Binary Tree Level Order Traversal
LeetCode Link: 102. Binary Tree Level Order Traversal
Given the root of a binary tree, return the level order traversal of its nodes’ values. (i.e., from left to right, level by level).
Analysis and Solution
Level Order Traversal
LeetCode C++ as followings Level Order Traversal
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
queue<TreeNode*> que;//define a queue to traverse tree
if (root != NULL) que.push(root);//push queue
vector<vector<int>> result;//define a result array
while (!que.empty()) {//queue is not empty
int size = que.size();//define a changeless size.
vector<int> vec;//define a vec
// use a changeless size. do not use que.size,cause que.size would be change
for (int i = 0; i < size; i++) {//traverse size
TreeNode* node = que.front();//front assign to node of tree
que.pop();//pop from queue
vec.push_back(node->val);//push_back() function is used to push elements into a vector from the back. The new value is inserted into the vector at the end, after the current last element and the container size is increased by 1.
if (node->left) que.push(node->left);//push left node into queue
if (node->right) que.push(node->right);//push right node into queue
}
result.push_back(vec);//push_back() function is used to push elements into a vector from the back. The new value is inserted into the vector at the end, after the current last element and the container size is increased by 1.
}
return result;
}
};
Recursion
LeetCode C++ as followings Recursion
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
void order(TreeNode* cur, vector<vector<int>>& result, int depth)
{
if (cur == nullptr) return;//return if current points to null pointer
if (result.size() == depth) result.push_back(vector<int>());//push_back() function is used to push elements into a vector from the back. The new value is inserted into the vector at the end, after the current last element and the container size is increased by 1. result push back if size of result is equal to depth of tree
result[depth].push_back(cur->val);//push back current value
order(cur->left, result, depth + 1);//recursion
order(cur->right, result, depth + 1);//recursion
}
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> result;//define a result
int depth = 0;//define depth of tree
order(root, result, depth);//recursion
return result;//show the result
}
};
2.Invert Binary Tree
LeetCode Link: 226. Invert Binary Tree
Given the root of a binary tree, invert the tree, and return its root.
Analysis and Solution
Recursion
LeetCode C++ as followings Recursion
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if (root == NULL) return root;//special situation 1,return root if it is a null
swap(root->left, root->right); // swap function to swap two value
invertTree(root->left); // recursion left child
invertTree(root->right); // recursion right child
return root;//return new tree
}
};
3.Symmetric Tree
LeetCode Link: 101. Symmetric Tree
Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).
Analysis and Solution
Recursion
LeetCode C++ as followings Recursion
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
bool compare(TreeNode* left, TreeNode* right) {
// remove situation of the null node
if (left == NULL && right != NULL) return false;
else if (left != NULL && right == NULL) return false;
else if (left == NULL && right == NULL) return true;
// remove the value isnot equal to each other
else if (left->val != right->val) return false;//else left and right isnot null, and they are equal to each other.
//recursion later for child of next level
bool outside = compare(left->left, right->right); // left child tree:left; right child tree: right
bool inside = compare(left->right, right->left); // left child tree:right; right child tree: left
bool isSame = outside && inside; // left child tree:middle; right child tree: middle(cope with logic)
return isSame;//show the key
}
bool isSymmetric(TreeNode* root) {
if (root == NULL) return true;//case 0 of special situation
return compare(root->left, root->right);//recursion
}
};