力扣labuladong——一刷day46

提示:文章寫完后,目錄可以自動生成,如何生成可參考右邊的幫助文檔

文章目錄

  • 前言
  • 一、力扣971. 翻轉二叉樹以匹配先序遍歷
  • 二、力扣987. 二叉樹的垂序遍歷
  • 三、力扣666. 路徑總和 IV


前言


二叉樹的遞歸分為「遍歷」和「分解問題」兩種思維模式,這道題需要用到「遍歷」的思維。

一、力扣971. 翻轉二叉樹以匹配先序遍歷

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode() {}*     TreeNode(int val) { this.val = val; }*     TreeNode(int val, TreeNode left, TreeNode right) {*         this.val = val;*         this.left = left;*         this.right = right;*     }* }*/
class Solution {List<Integer> res = new ArrayList<>();int[] voyageArr;int i = 0;boolean flag = true;public List<Integer> flipMatchVoyage(TreeNode root, int[] voyage) {voyageArr = voyage;fun(root);if(flag == false){List<Integer> list = new ArrayList<>();list.add(-1);return list;}return res;}public void fun(TreeNode root){if(root == null){return;}if(root.val != voyageArr[i++]){flag = false;return;}if(root.left != null && root.left.val != voyageArr[i]){res.add(root.val);TreeNode temp = root.left;root.left = root.right;root.right = temp;}fun(root.left);fun(root.right);}
}

二、力扣987. 二叉樹的垂序遍歷

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode() {}*     TreeNode(int val) { this.val = val; }*     TreeNode(int val, TreeNode left, TreeNode right) {*         this.val = val;*         this.left = left;*         this.right = right;*     }* }*/
class Solution {class Triple{TreeNode node;int row, col;public Triple(TreeNode node, int row, int col){this.node = node;this.row = row;this.col = col;}}LinkedList<List<Integer>> res = new LinkedList<>();List<Triple> nodes = new ArrayList<>();public List<List<Integer>> verticalTraversal(TreeNode root) {traverse(root,0,0);Collections.sort(nodes, (tri1,tri2)->{if(tri1.col != tri2.col){return tri1.col - tri2.col;}else if(tri1.row != tri2.row){return tri1.row - tri2.row;}else{return tri1.node.val - tri2.node.val;}});int pre = Integer.MIN_VALUE;for(int i = 0; i < nodes.size(); i ++){Triple cur = nodes.get(i);if(cur.col != pre){res.addLast(new LinkedList<Integer>());pre = cur.col;}res.getLast().add(cur.node.val);}return res;}public void traverse(TreeNode root, int row, int col){if(root == null){return ;}nodes.add(new Triple(root,row,col));traverse(root.left, row+1, col-1);traverse(root.right, row+1, col + 1);}
}

三、力扣666. 路徑總和 IV

class Solution {Map<Integer,Integer> tree = new HashMap<>();int sum = 0;public int pathSum(int[] nums) {for(int i = 0; i < nums.length; i ++){int value = nums[i]%10;int code = nums[i]/10;tree.put(code,value);}int rootCode = nums[0]/10;fun(rootCode,0);return sum;}public void fun(int code, int path){if(!tree.containsKey(code)){return;}int value = tree.get(code);int[] pos = decode(code);int depth = pos[0], id = pos[1];int leftCode = encode(depth+1, 2*id-1);int rightCode = encode(depth+1,2*id);if(!tree.containsKey(leftCode) && !tree.containsKey(rightCode)){sum += path + value;return;}fun(leftCode, path + value);fun(rightCode, path + value);}public int[] decode(int code){int id = code%10;int depth = code/10;return new int[]{depth,id};}public int encode(int depth, int id){return depth*10 + id;}
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/164411.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/164411.shtml
英文地址,請注明出處:http://en.pswp.cn/news/164411.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

面試:RocketMQ相關問題

文章目錄 什么是 RocketMQ&#xff0c;有哪些使用場景&#xff1f;RocketMQ 由哪些?色組成&#xff0c;每個?色作用和特點是什么&#xff1f;RocketMQ 中的 Topic 和 JMS 的 queue 有什么區別&#xff1f;RocketMQ 消費模式有幾種&#xff1f;RocketMQ 的 Consumer 是如何消費…

【深度學習】Python快捷調用InsightFace人臉檢測,純ONNX推理

pypi資料&#xff1a; https://pypi.org/project/insightface/ 模型選擇&#xff1a; https://github.com/deepinsight/insightface/tree/master/python-package#model-zoo onnxruntime的GPU對應CUDA &#xff1a; https://onnxruntime.ai/docs/reference/compatibility …

1999-2021年地級市城鎮居民人均消費性支出數據

1999-2021年地級市城鎮居民人均消費性支出數據 1、時間&#xff1a;1999-2021年 2、指標&#xff1a;城鎮居民人均消費性支出 3、范圍&#xff1a;290個地級市 4、來源&#xff1a;城市年鑒、地級市統計公報 5、指標解釋&#xff1a; 城鎮居民人均消費性支出&#xff1a;指…

kubesphere安裝依賴文件

yum install socat -y yum install conntrack -y

GAMES101-Homework2

目錄 普通作業&#xff1a;提高作業&#xff1a;參考博客博客一博客二博客三 附代碼框架的個人一些注釋和理解&#xff1a;rasterizer.cppTriangle.cpp 普通作業&#xff1a; // 判斷點是否在三角形內的輔助函數 static bool insideTriangle(float x, float y, const Vector3f…

再添千萬級罰單,某銀行年內罰款過億!金融行業合規問題亟待解決

11月17日晚間&#xff0c;國家金融監管總局上海監管局披露行政處罰信息顯示&#xff0c;某銀行因32項違法違規事實收到兩張690萬元的大額罰單&#xff0c;合計罰款金額達1380萬元。但這并不是銀行該今年收到的第一張大額罰單。今年4月28日&#xff0c;該行因在結售匯、外幣理財…

k8s-pod生命周期 4

容器環境初始化 pod 由pod 鏡像來提供&#xff0c;在pod 生命周期里容器主要分為兩種&#xff1a;初始化容器和主容器 初始化容器一定要成功運行并退出&#xff0c;當初始化容器運行退出完了之后主容器開始和運行 主容器開始運行的時候&#xff0c;有兩個探針&#xff1a;存…

什么是arguments對象?

arguments 對象是 JavaScript 中的一個特殊對象&#xff0c;它包含了函數被調用時傳入的所有參數。arguments 對象是一個類數組對象&#xff0c;它有一個 length 屬性和按數字索引的元素。 每個函數在執行時都會自動創建一個 arguments 對象。我們可以通過arguments去訪問參數…

網絡圖簡單計算規則

單代號進度網絡圖&#xff08;節點法&#xff09; 概念 計算規則 &#xff08;順時針計算法&#xff09; &#xff08;TF取之差&#xff09; &#xff08;T&#xff1a;持續時間&#xff09; ES → EF (ES取大EF加T) ↑ T ↑ &#xff08;TF&#xff1a;總時差&…

NOIP2003提高組第二輪T3:加分二叉樹

題目鏈接 [NOIP2003 提高組] 加分二叉樹 題目描述 設一個 n n n 個節點的二叉樹 tree \text{tree} tree 的中序遍歷為 ( 1 , 2 , 3 , … , n ) (1,2,3,\ldots,n) (1,2,3,…,n)&#xff0c;其中數字 1 , 2 , 3 , … , n 1,2,3,\ldots,n 1,2,3,…,n 為節點編號。每個節點都…

【視覺SLAM十四講學習筆記】第三講——Eigen庫

專欄系列文章如下&#xff1a; 【視覺SLAM十四講學習筆記】第一講——SLAM介紹 【視覺SLAM十四講學習筆記】第二講——初識SLAM 【視覺SLAM十四講學習筆記】第三講——旋轉矩陣 本章將介紹視覺SLAM的基本問題之一&#xff1a;如何描述剛體在三維空間中的運動&#xff1f; Eigen…

網工內推 | Base北京,國企網工運維,最高30k*14薪,IE認證優先

01 萬方數據股份有限公司 招聘崗位&#xff1a;網絡工程師 職責描述&#xff1a; 1.負責完成基礎網絡組網工作&#xff1b; 2.負責網絡對象的訪問控制及安全策略&#xff0c;配置VLan&#xff0c;黑白名單、地址轉換、故障排查及網絡安全監控工作&#xff1b; 3.負責對操作系…

Vue框架學習筆記——Vue實例中el和data的兩種寫法

文章目錄 前文提要Vue實例的el第一種寫法第二種寫法小結 Vue實例中data第一種寫法&#xff0c;對象式效果圖片第二種寫法&#xff0c;函數式效果圖片小結 前文提要 本文僅做自己的學習記錄&#xff0c;如有錯誤&#xff0c;請多諒解 Vue實例的el 第一種寫法 <body><…

Python圖片文件和base64編碼互轉

圖片和base64編碼互轉 import base64 import cv2# 將圖片base64字符串生成圖片文件. def base64_to_img(base64_code,save_img_path):"""根據base64生成圖片.:param base64_code: 圖片的base64文件:param save_img_path: 生成的圖片路徑:returns: None"&q…

分布式鎖之基于mysql實現分布式鎖(四)

不管是jvm鎖還是mysql鎖&#xff0c;為了保證線程的并發安全&#xff0c;都提供了悲觀獨占排他鎖。所以獨占排他也是分布式鎖的基本要求。 可以利用唯一鍵索引不能重復插入的特點實現。設計表如下&#xff1a; CREATE TABLE tb_lock (id bigint(20) NOT NULL AUTO_INCREMENT,…

(二)C語言之變量與算數運算表達式概述

C語言之變量與算數運算表達式概述 一、華氏溫度與攝氏溫度對照二、代碼概述三、練習 一、華氏溫度與攝氏溫度對照 #include <stdio.h>/*當華氏溫度為 0,20,40,...300時&#xff0c;打印出華氏溫度與攝氏溫度對照表華氏溫度與攝氏溫度 C(5/9)(?F-32) 其中C表示攝氏溫度&…

順序棧和鏈棧

#include<iostream> using namespace std; #define MAXSIZE 100 typedef int SElemType; typedef struct { SElemType* base; SElemType* top; int stacksize; }SqStack;//順序棧 //構造一個空棧 int InitStack(SqStack& s) { s.base new SElemType…

Django之中間件與CSRF_TOKEN

文章目錄 一、什么是中間件二、中間件有什么用三、Django自定義中間件中間件中主要方法及作用創建自定義中間件的步驟&#xff1a;process_request與process_response方法process_view方法process_exceptionprocess_template_response&#xff08;不常用&#xff09; 四、CSRF_…

mysql latin-1報錯解決

conn pymysql.connect(hostmeta_conf[host], usermeta_conf[user], passwordmeta_conf[password], portmeta_conf[port], charsetutf8) 光把表聲明 ENGINEINNODB DEFAULT CHARSETutf8mb4 COLLATEutf8mb4_bin ROW_FORMATDYNAMIC 并不能解決這個報錯,需要在創建mysql連接時候…

面試:RabbitMQ相關問題

文章目錄 簡單介紹RabbitMQRabbitMQ架構什么是 RabbitMQ&#xff1f;有什么顯著的特點&#xff1f;RabbitMQ 有那些基本概念&#xff1f;RabbitMQ routing 路由模式消息怎么路由&#xff1f;RabbitMQ publish/subscribe 發布訂閱(共享資源)能夠在地理上分開的不同數據中心使用 …