Journey of LeetCode|DAY 15
Preface
This is a new day to continue my binary tree journey.
Learn something new and keep reviewing what I learnt before.
1. Balanced Binary Tree
LeetCode Link: 110. Balanced Binary Tree
Given a binary tree, determine if it is height-balanced.
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:
int getHeight(TreeNode* node) {//define a function to get height of tree
if (node == NULL) {//remove the null tree situation
return 0;
}
int leftHeight = getHeight(node->left); // recursion left tree
if (leftHeight == -1) return -1;//return -1 if not balanced tree
int rightHeight = getHeight(node->right); // recursion right tree
if (rightHeight == -1) return -1;//return -1 if not balanced tree
int result;
if (abs(leftHeight - rightHeight) > 1) { // return -1 if not balanced tree
result = -1;
} else {
result = 1 + max(leftHeight, rightHeight); // maxheight of this tree formed by current node
}
return result;
}
bool isBalanced(TreeNode* root) {//main function
return getHeight(root) == -1 ? false : true;//recursion; result=-1, fasle; result=maxheight,true
}
};
2. Binary Tree Paths
LeetCode Link: 257. Binary Tree Paths
Given the root of a binary tree, return all root-to-leaf paths in any order.
A leaf is a node with no children.
Analysis and Solution
Recursion And Backtrack
LeetCode C++ as followings Recursion And Backtrack
/**
* 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 {
private:
void traversal(TreeNode* cur, vector<int>& path, vector<string>& result) {//recursion function; root path result
path.push_back(cur->val); // middle node; add it(last node) into path
// 这才到了叶子节点
if (cur->left == NULL && cur->right == NULL) {//stop recursion logic
string sPath;//define a sPath(string)
for (int i = 0; i < path.size() - 1; i++) {//convert all the pathes to string type
sPath += to_string(path[i]);
sPath += "->";
}
sPath += to_string(path[path.size() - 1]);//record the last node
result.push_back(sPath);//add the path
return;
}
if (cur->left) { // recursion left node
traversal(cur->left, path, result);
path.pop_back(); // backtrack
}
if (cur->right) { // recursion right node
traversal(cur->right, path, result);
path.pop_back(); // backtrack
}
}
public:
vector<string> binaryTreePaths(TreeNode* root) {//main function
vector<string> result;//define a recult array string type
vector<int> path;//path array ,type int
if (root == NULL) return result;//remove situation root=null
traversal(root, path, result);//resursion
return result;
}
};
3. Sum of Left Leaves
LeetCode Link: 404. Sum of Left Leaves
Given the root of a binary tree, return the sum of all left leaves.
A leaf is a node with no children. A left leaf is a leaf that is the left child of another node.
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:
int sumOfLeftLeaves(TreeNode* root) {
if (root == NULL) return 0;//return 0 without left leaves
if (root->left == NULL && root->right== NULL) return 0;//return 0 without left leaves
int leftValue = sumOfLeftLeaves(root->left); // left node
if (root->left && !root->left->left && !root->left->right) { // left tree is a sigle left leaves
leftValue = root->left->val;//record value
}
int rightValue = sumOfLeftLeaves(root->right); // right node
int sum = leftValue + rightValue; // sum of all the left leaves
return sum;
}
};