首页 > 其他分享 >二叉树(1)

二叉树(1)

时间:2024-01-29 19:22:47浏览次数:27  
标签:right back traversal 二叉树 path root left

目录

前面一些简单题就没放上来,放的都是一开始没思路的

110平衡二叉树

显然这题不能单纯的返回true false 还需要把这一层的高度接住
所以用-1作为标识符,如果=-1说明下层已经有不平衡了,那么都返回-1
否则就返回这棵树的高度

class Solution {
public:
    int getDepth(TreeNode * root)
    {
        if(root==nullptr)
        {
            return 0;
        }
        int left=getDepth(root->left);
        int right=getDepth(root->right);
        if(left==-1||right==-1)
        {
            return -1;
        }
        if(abs(left-right)>1)
        {
            return -1;
        }
        return max(left,right)+1;
    }
    bool isBalanced(TreeNode* root) {
        return !(getDepth(root)==-1);
    }
};

257二叉树的所有路径

能过,但是感觉不优雅

class Solution {
public:
    vector<string> ans;
    vector<TreeNode*> path;
    void traversal(TreeNode* root)
    {
        if(!root->left&&!root->right)
        {
            string temp;
            for(int i=0;i<path.size();i++)
            {
                temp+=to_string(path[i]->val)+"->";
            }
            temp+=to_string(root->val);
            ans.push_back(temp);
        }
        if(root->left)
        {
            path.push_back(root);
            traversal(root->left);
            path.pop_back();
        }
        if(root->right)
        {
            path.push_back(root);
            traversal(root->right);
            path.pop_back();
        }
    }
    vector<string> binaryTreePaths(TreeNode* root) {
        traversal(root);
        return ans;

    }
};

这下舒服多了

class Solution {
public:
    vector<string> ans;
    vector<int> path;
    void traversal(TreeNode* root)
    {
        path.push_back(root->val);
        if(!root->left&&!root->right)
        {
            string temp;
            for(int i=0;i<path.size()-1;i++)
            {
                temp+=to_string(path[i])+"->";
            }
            temp+=to_string(root->val);
            ans.push_back(temp);
        }
        if(root->left)
        {
            
            traversal(root->left);
            path.pop_back();
        }
        if(root->right)
        {
            traversal(root->right);
            path.pop_back();
        }
    }
    vector<string> binaryTreePaths(TreeNode* root) {
        traversal(root);
        return ans;

    }
};

标签:right,back,traversal,二叉树,path,root,left
From: https://www.cnblogs.com/liviayu/p/17995164

相关文章

  • 【树】二叉树的应用 I
    目录1.题目列表2.应用2.1.Leetcode226.翻转二叉树2.1.1.题目2.1.2.解题思路2.1.2.1.方法一:前序遍历2.1.2.2.方法二:后序遍历2.1.3.代码实现2.2.Leetcode116.填充每个节点的下一个右侧节点指针2.2.1.题目2.2.2.解题思路2.2.2.1.方法一:广度优先搜索2.2.2.2.方法二:深......
  • 如何学习算法:什么时完全二叉树?完全二叉树有什么特点?
    完全二叉树我们知道树是一种非线性数据结构。它对儿童数量没有限制。二叉树有一个限制,因为树的任何节点最多有两个子节点:左子节点和右子节点。什么是完全二叉树?完全二叉树是一种特殊类型的二叉树,其中树的所有级别都被完全填充,除了最低级别的节点从尽可能左侧填充之外。完全二叉树的......
  • 【树】二叉树的应用 I
    目录1.题目2.应用2.1.Leetcode124.二叉树中的最大路径和2.1.1.题目2.1.2.解题思路2.1.3.代码实现1.题目二叉树相关的题目:序号题目难度1124.二叉树中的最大路径和困难22.应用2.1.Leetcode124.二叉树中的最大路径和2.1.1.题目124.二叉树......
  • KY11 二叉树遍历C++
    这个题目思路其实就是先序遍历的变形。相当于沿着先序遍历的顺序跟着构建二叉树就行。然后中序遍历这个树。#include<iostream>#include<string>usingnamespacestd;structtnode{chardata;structtnode*left;structtnode*right;};typedefstructt......
  • KY212 二叉树遍历C++
    思路是先构造出树,然后在后序遍历整个树。#include<iostream>#include<string>usingnamespacestd;structTnode{chardata;structTnode*left;structTnode*right;};typedefstructTnodeTree;Tree*build(stringpre,inth1,intt1,stringin,inth2......
  • KY85 二叉树C++
    递归判断当前节点和n的关系就好了。如果小于等于n那就是存在。#include<iostream>usingnamespacestd;intcount(inti,intn){if(i>n)return0;returncount(2*i,n)+count(2*i+1,n)+1;}intmain(){intn,m;while(cin>>m>>n){if(n==0)......
  • 遍历二叉树非递归实现
    实现1.前序遍历publicvoidpreOrderNor(TreeNoderoot){if(root==null){return;}Stack<TreeNode>stack=newStack<>();stack.push(root);while(!stack.isEmpty()){TreeNodecur......
  • 二叉树面试题进阶
    二叉树面试题进阶1.二维数组存储层序遍历结果难点: 如何存储每一层的节点?根据队列节点的个数判断每一层classSolution{publicList<List<Integer>>levelOrder(TreeNoderoot){List<List<Integer>>retList=newArrayList<>();if(root==nu......
  • 实现创建二叉树
    创建二叉树1.前序遍历创建二叉树importjava.util.Scanner;//注意类名必须为Main,不要有任何packagexxx信息classTreeNode{publicTreeNodeleft;publiccharval;publicTreeNoderight;publicTreeNode(charval){this.val=......
  • 最少交换次数 置换环 LeetCode 2471. 逐层排序二叉树所需的最少操作数目
    voidMain(){ varroot=newTreeNode(1) { left=newTreeNode(3) { left=newTreeNode(7), right=newTreeNode(6) }, right=newTreeNode(2) { left=newTreeNode(5), right=newTreeNode(4) } }; varr=newSolution().Minimu......