博客
关于我
牛客网算法——名企高频面试题143题(11)
阅读量:393 次
发布时间:2019-03-04

本文共 3524 字,大约阅读时间需要 11 分钟。

题目描述

给定一个二叉树,请计算节点值之和最大的路径的节点值之和是多少。

这个路径的开始节点和结束节点可以是二叉树中的任意节点

package 复现代码;/** * @Classname 二叉树的路径和 * @Description TODO * @Date 2020/12/19 15:23 * @Created by xjl */public class 二叉树的路径和 {       int res = Integer.MIN_VALUE;    public int TreeMax(TreeNode root) {           if (root == null) {               return 0;        }        getMax(root);        return res;    }    private int getMax(TreeNode root) {           if (root == null) {               return 0;        }        int L = Math.max(0, getMax(root.left));        int R = Math.max(0, getMax(root.right));        res = Math.max(res, Math.max(root.val + Math.max(L, R), root.val + R + L));        return Math.max(L, R) + root.val;    }    public class TreeNode {           int val = 0;        TreeNode left = null;        TreeNode right = null;    }}

二叉树的直径

package 复现代码;/** * @Classname 二叉树的直径II * @Description TODO * @Date 2020/12/19 15:35 * @Created by xjl */public class 二叉树的直径II {       public class TreeNode {           int val;        TreeNode left;        TreeNode right;        public TreeNode(int val) {               this.val = val;        }    }    int maxD = 1;    public int MaxD(TreeNode root) {           if (root == null) {               return 0;        }        getD(root);        return maxD-1;    }    private int getD(TreeNode root) {           if (root == null) {               return 0;        }        int L = getD(root.left);        int R = getD(root.right);        maxD = Math.max(maxD, L + R + 1);        return Math.max(L, R) + 1;    }}

题目描述

将给定的单链表 L\ L L: L0→L1→…→Ln−1→LnL_0→L_1→…→L_{n-1}→L_ nL0​→L1​→…→Ln−1​→Ln​

重新排序为:L0→Ln→L1→Ln−1→L2→Ln−2→…L_0→L_n →L_1→L_{n-1}→L_2→L_{n-2}→…L0​→Ln​→L1​→Ln−1​→L2​→Ln−2​→…
要求使用原地算法,不能改变节点内部的值,需要对实际的节点进行交换。
例如:
对于给定的单链表{10,20,30,40},将其重新排序为{10,40,20,30}.

package 名企高频面试题143;import org.junit.Test;/** * @Classname 链表的排序 * @Description TODO * @Date 2020/12/19 15:53 * @Created by xjl */public class 链表的排序 {       public class ListNode {           int val;        ListNode next;        ListNode(int x) {               val = x;            next = null;        }    }    public void reorderList(ListNode head) {           if(head == null || head.next == null || head.next.next == null) {               return ;        }        //快慢指针,找到中间节点        ListNode fast = head;        ListNode slow = head;        while(fast != null && fast.next != null){               slow = slow.next;            fast = fast.next.next;        }        ListNode mid = slow;        ListNode start = head;        ListNode end1 = mid.next;        //断链        mid.next = null;        //链表二进行翻转        ListNode end = reverList(end1);        //插入        while(start != null && end!=null){               ListNode next1 = start.next;            ListNode next2 = end.next;            start.next = end;            end.next = next1;            start = next1;            end = next2;        }        return ;    }    private ListNode reverList(ListNode head) {           ListNode pre = null;        ListNode curr = head;        while (curr != null) {               ListNode next = curr.next;            curr.next = pre;            pre = curr;            curr = next;        }        return pre;    }    @Test    public void test() {           ListNode root = new ListNode(1);        ListNode s1 = new ListNode(2);        ListNode s2 = new ListNode(3);        ListNode s3 = new ListNode(4);//        ListNode s4 = new ListNode(5);        root.next = s1;        s1.next = s2;        s2.next = s3;//        s3.next = s4;        reorderList(root);    }}

转载地址:http://emch.baihongyu.com/

你可能感兴趣的文章
NIFI大数据进阶_NIFI集群知识点_认识NIFI集群以及集群的组成部分---大数据之Nifi工作笔记0014
查看>>
NIFI大数据进阶_NIFI集群知识点_集群的断开_重连_退役_卸载_总结---大数据之Nifi工作笔记0018
查看>>
NIFI大数据进阶_内嵌ZK模式集群1_搭建过程说明---大数据之Nifi工作笔记0015
查看>>
NIFI大数据进阶_外部ZK模式集群1_实际操作搭建NIFI外部ZK模式集群---大数据之Nifi工作笔记0017
查看>>
NIFI大数据进阶_离线同步MySql数据到HDFS_01_实际操作---大数据之Nifi工作笔记0029
查看>>
NIFI大数据进阶_离线同步MySql数据到HDFS_02_实际操作_splitjson处理器_puthdfs处理器_querydatabasetable处理器---大数据之Nifi工作笔记0030
查看>>
NIFI大数据进阶_连接与关系_设置数据流负载均衡_设置背压_设置展现弯曲_介绍以及实际操作---大数据之Nifi工作笔记0027
查看>>
NIFI数据库同步_多表_特定表同时同步_实际操作_MySqlToMysql_可推广到其他数据库_Postgresql_Hbase_SqlServer等----大数据之Nifi工作笔记0053
查看>>
NIFI汉化_替换logo_二次开发_Idea编译NIFI最新源码_详细过程记录_全解析_Maven编译NIFI避坑指南001---大数据之Nifi工作笔记0068
查看>>
NIFI集群_内存溢出_CPU占用100%修复_GC overhead limit exceeded_NIFI: out of memory error ---大数据之Nifi工作笔记0017
查看>>
NIFI集群_队列Queue中数据无法清空_清除队列数据报错_无法删除queue_解决_集群中机器交替重启删除---大数据之Nifi工作笔记0061
查看>>
NIH发布包含10600张CT图像数据库 为AI算法测试铺路
查看>>
Nim教程【十二】
查看>>
Nim游戏
查看>>
NIO ByteBuffer实现原理
查看>>
Nio ByteBuffer组件读写指针切换原理与常用方法
查看>>
NIO Selector实现原理
查看>>
nio 中channel和buffer的基本使用
查看>>
NIO三大组件基础知识
查看>>
NIO与零拷贝和AIO
查看>>