机票便宜 网站建设,云南网官方网站,外贸网站平台推广,红旗网站建设leetcode 150道题 计划花两个月时候刷完#xff0c;今天#xff08;第三十天#xff09;完成了3道(62-64)150#xff1a;
62.#xff08;226. 翻转二叉树#xff09;题目描述#xff1a;
给你一棵二叉树的根节点 root #xff0c;翻转这棵二叉树#xff0c;并返回其…leetcode 150道题 计划花两个月时候刷完今天第三十天完成了3道(62-64)150
62.226. 翻转二叉树题目描述
给你一棵二叉树的根节点 root 翻转这棵二叉树并返回其根节点。第一版直接递归把每一个节点当做一个新二叉树去对待
class Solution {public TreeNode invertTree(TreeNode root) {swapTree(root);return root;}public void swapTree(TreeNode root) {if(rootnull){return ;}TreeNode leftroot.left;TreeNode rightroot.right;root.leftright;root.rightleft;swapTree(left);swapTree(right);}
}63.101. 对称二叉树题目描述
给你一个二叉树的根节点 root 检查它是否轴对称。第一版还是递归先把节点分为两个左节点和右节点当作两个新树去比较左二叉树和右二叉树镜像翻过来时候相等
class Solution {public boolean isSymmetric(TreeNode root) {if(rootnull){return false;}TreeNode leftroot.left;TreeNode rightroot.right;return compareTree(left,right);}public boolean compareTree(TreeNode left,TreeNode right){if(leftnullrightnull){return true;}if(left!nullright!nullleft.valright.val){return compareTree(left.right,right.left)compareTree(left.left,right.right);}return false;}
}64.105. 从前序与中序遍历序列构造二叉树题目描述
给定两个整数数组 preorder 和 inorder 其中 preorder 是二叉树的先序遍历 inorder 是同一棵树的中序遍历请构造二叉树并返回其根节点。第一版经典题目学数据结构时候应该都遇到过但是我只是当时写过学完后经常碰到但是没勇气和耐心再去写一遍。。今天不得不写
class Solution {MapInteger,Integer mapnew HashMap();public TreeNode buildTree(int[] preorder, int[] inorder) {for(int i0;iinorder.length;i){map.put(inorder[i],i);}return buildSubTree(preorder,0,preorder.length-1,inorder,0,inorder.length-1);}public TreeNode buildSubTree(int[] preorder, int pLeft,int pRight,int[] inorder,int iLeft,int iRight) {if(pLeftpRight||iLeftiRight){return null;}if(pLeftpRight){return new TreeNode(preorder[pLeft]);}TreeNode rootnew TreeNode(preorder[pLeft]);int rootIndexmap.get(preorder[pLeft]);int countrootIndex-iLeft;root.leftbuildSubTree(preorder,pLeft1,pLeftcount,inorder,iLeft,rootIndex-1);root.rightbuildSubTree(preorder,pLeft1count,pRight,inorder,rootIndex1,iRight);return root;}
}今天有点发懒了。。差点不想打开电脑。。还好还好今天真的最后一个我是看了一下讲解然后自己就写了一版过了就是在处理找中序的坐标时候我没想到先把中序的用map保存一遍。。其他的和解题的递归一模一样很有成就感
第三十天了不知道刷题对找工作有没有帮助。。但是也不知道干啥了这几天工作活感觉要上强度了mmp加油希望能早日跳槽吧