LeetCode DAY13(102. Binary Tree Level Order Traversal&226. Invert Binary Tree&101. Symmetric Tree)

二叉树遍历与操作:层次遍历、翻转与对称性检查

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
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值