LeetCode DAY15(110. Balanced Binary Tree&257. Binary Tree Paths&404. Sum of Left Leaves)

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;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值