【树-简单】面试题 17.12. BiNode

本文介绍了一种将二叉搜索树转换为单向链表的方法,通过原地修改二叉树来保持二叉搜索树的特性。提供两种实现方式:递归与迭代。

题目
二叉树数据结构TreeNode可用来表示单向链表(其中left置空,right为下一个链表节点)。实现一个方法,把二叉搜索树转换为单向链表,要求依然符合二叉搜索树的性质,转换操作应是原址的,也就是在原始的二叉搜索树上直接修改。

返回转换后的单向链表的头节点。

注意:本题相对原题稍作改动

示例:

输入: [4,2,5,1,3,null,6,0]
输出: [0,null,1,null,2,null,3,null,4,null,5,null,6]

提示:

节点数量不会超过 100000。

【代码】
【迭代】
在这里插入图片描述

class Solution:
    def convertBiNode(self, root: TreeNode) -> TreeNode:
        if not root:
            return root
        stack=[]
        cur=None
        lastHead=None
        while stack or root:
            while root:
                stack.append(root)
                root=root.left
            root=stack.pop()
            if not cur:
                cur=lastHead=root
            else:
                cur.right=root
                cur=root
            root.left=None
            root=root.right
        return lastHead

【递归】
在这里插入图片描述

class Solution:
    def visit(self,root):
        if not root:
            return
        self.visit(root.left)
        if not self.cur:
            self.cur=self.lastNode=root
        else:
            self.cur.right=root
            self.cur=root
        root.left=None
        self.visit(root.right)

    def convertBiNode(self, root: TreeNode) -> TreeNode:
        self.lastNode=None
        self.cur=None
        self.visit(root)
        return self.lastNode
<template> <van-tabbar v-model="active" :placeholder="true" :route="true" fixed> <van-tabbar-item replace v-for="(item, index) in tabbarData" :key="index" :icon="item.icon" :to="item.to"> <span class="tabbar_sp">{{ item.title }}</span> <template #icon="props"> <div class="tabbar_icon" v-html="getSvgIcon(item.icon, props.active)"></div> </template> </van-tabbar-item> </van-tabbar> </template> <script setup> const { t } = useI18n(); const active = ref(0); const tabbarData = reactive([ { icon: `<svg t="1760085025618" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1686" width="20" height="20"><path d="M611.15 627.3c-25.55 24.06-58.96 37.31-94.08 37.31-35.42 0-69.04-13.45-94.67-37.87-12.39-11.81-32.02-11.34-43.83 1.06-11.81 12.39-11.34 32.02 1.06 43.83 37.21 35.46 86.02 54.99 137.44 54.99 50.98 0 99.48-19.24 136.58-54.17 12.46-11.74 13.05-31.36 1.32-43.82-11.74-12.48-31.36-13.07-43.82-1.33z" fill="#333333" p-id="1687"></path><path d="M784.43 306.03L590.75 190.91a141.649 141.649 0 0 0-144.84 0L252.22 306.03c-42.76 25.42-69.32 72.1-69.32 121.84v286.15c0 78.16 63.58 141.74 141.74 141.74H712c78.16 0 141.74-63.58 141.74-141.74V427.87c0.01-49.74-26.55-96.43-69.31-121.84z m7.32 407.98c0 43.97-35.77 79.74-79.74 79.74H324.65c-43.97 0-79.74-35.77-79.74-79.74V427.87c0-27.98 14.94-54.25 39-68.55L477.58 244.2c12.57-7.47 26.65-11.2 40.74-11.2s28.18 3.73 40.74 11.2l193.68 115.12c24.05 14.3 39 40.56 39 68.55v286.14z" fill="#333333" p-id="1688"></path></svg>`, title: computed(() => t("home.tabbar.home")), to: { name: "home" } }, { icon: `<svg t="1760085095047" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1847" width="20" height="20"><path d="M873.8 624.7l-24.2-108.3c-7.9-35.5-7.4-72.8 1.6-108.1l21.5-84.4c10.8-42.7-4.6-87-39.4-113-18.4-13.7-40.1-21-62.9-21H692c-18.4-32.9-53.5-55.2-93.8-55.2h-331c-59.2 0-107.3 48.1-107.3 107.3v601.8c0 17.1 13.9 31 31 31s31-13.9 31-31V700.5h345.5c14.6 32.5 46.6 55.2 83.7 55.2h119.1c33.6 0 65.8-16.6 85.9-44.3 18-24.7 24.4-56.3 17.7-86.7zM221.9 394.2V242.1c0-25 20.3-45.3 45.3-45.3h331c25 0 45.3 20.3 45.3 45.3v351.1c0 25-20.3 45.3-45.3 45.3H221.9V394.2z m584.2 280.7c-8.7 11.9-21.7 18.8-35.8 18.8H651.2c-3.6 0-7.1-0.7-10.4-2 38.1-16.5 64.8-54.4 64.8-98.5V252h64.7c9.3 0 18.2 3 25.8 8.7 14.6 10.9 21 29.7 16.4 48L791 393.1c-11.3 44.6-12 91.9-2 136.9l24.2 108.3c3 12.9 0.3 26.3-7.1 36.6z" fill="#333333" p-id="1848"></path></svg>`, title: computed(() => t("home.tabbar.task")), to: { name: "task" } }, { icon: `<svg t="1760085118582" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2007" width="20" height="20"><path d="M495.19 190.04a229.893 229.893 0 0 1 38.03 33.14c21.08 22.89 38.1 50.57 50.61 82.27 14.41 36.51 19.34 69.65 21 86.16l7.82 77.81c1.66 16.56 22.99 24.05 35.9 12.62l60.66-53.75 4.63-4.1c30.78 39.45 52.65 76.76 65.23 111.33 7.66 21.06 11.89 41.28 12.56 60.1 0.64 17.99-1.81 34.62-7.51 50.86-8.43 24.03-24.27 46.76-47.08 67.56-17.33 15.81-38.63 30.52-63.42 43.82 9.91-61.31-11.76-114.22-62.58-151.02-19.07-13.81-34.18-28.53-44.91-43.76-7.31-10.37-12.46-20.75-15.31-30.86-1.61-5.7-2.49-11.26-2.63-16.5l-1.77-68.38c-0.4-15.64-19.01-24.77-33-16.21l-61.18 37.46C370.57 518.6 323.7 580 312.92 651.09c-3.87 25.51-2.61 50.08 1.84 73.04-5.69-4.48-11.05-9.01-16.06-13.57-31.38-28.54-48.65-57.37-52.8-88.13-4.55-33.75 7.1-70.56 34.65-109.42 29.27-41.3 66.63-71.35 86.28-85.58 50.8-36.79 89.86-100.55 116.1-189.51 4.98-16.87 9.02-33.12 12.26-47.88m-48.57-88.56l-1.24 12.77c-0.19 1.97-20.24 197.62-116.51 267.34-22.68 16.42-65.82 51.17-100.01 99.39-35.98 50.76-50.95 100.8-44.48 148.73 11.85 87.84 95.18 163.62 247.67 225.24l0.03-0.04c6.66-8.88 6.36-20.76-0.68-29.37-22.73-27.78-68.08-93.69-57.08-166.28 8.17-53.89 45.78-101.64 111.89-142.12 0.21 8.11 1.39 18.28 4.66 29.89 7.47 26.46 27.79 66.27 82.22 105.68 38.07 27.57 49.64 66.98 34.39 117.15-11.87 39.05-36.13 70.17-36.38 70.48-5.73 7.26 1.36 17.25 10.74 15.13h0.01c2.15-0.49 53.3-12.23 111.2-41.35 34.17-17.19 63.55-36.88 87.3-58.54 30.03-27.39 51.13-58 62.71-91 31.74-90.44-7.19-197.47-115.71-318.12l-7.06-7.85-53.7 47.58c-2.23-22.24-8.27-59.53-24.56-100.82-15.08-38.22-35.87-71.88-61.8-100.04-32.52-35.32-73.17-61.98-120.82-79.23l-12.79-4.62z" fill="#333333" p-id="2008"></path></svg>`, title: computed(() => t("home.tabbar.promotion")), to: { name: "promotion" } }, { icon: `<svg t="1760084905289" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1525" width="20" height="20"><path d="M674.77 315.62v-96.51c0-26.03-21.1-47.13-47.13-47.13H475.65c-161.68 0-292.74 131.06-292.74 292.74v253.09c0 76.29 61.85 138.14 138.14 138.14H715.4c76.29 0 138.14-61.85 138.14-138.14V439.77L674.77 315.62z m-68.4 471.94H318.69c-37.22 0-67.39-30.17-67.39-67.39V464.73c0.09-123.86 100.48-224.25 224.34-224.34h130.72v547.17z m178.9-67.24c-0.08 37.16-30.23 67.24-67.39 67.24h-40.21V403.51l108.14 69.94-0.54 246.87z" fill="#333333" p-id="1526"></path><path d="M399.72 416c-18.89 0-34.2 15.31-34.2 34.2s15.31 34.2 34.2 34.2h69.77c18.89 0 34.2-15.31 34.2-34.2s-15.31-34.2-34.2-34.2h-69.77zM469.49 555.87h-69.77c-18.89 0-34.2 15.31-34.2 34.2s15.31 34.2 34.2 34.2h69.77c18.89 0 34.2-15.31 34.2-34.2s-15.31-34.2-34.2-34.2z" fill="#333333" p-id="1527"></path></svg>`, title: computed(() => t("home.tabbar.Introduction")), to: { name: "Introduction" } }, { icon: `<svg t="1760085148297" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2167" width="20" height="20"><path d="M509.77 540.64c111.78 0 202.72-90.94 202.72-202.72S621.55 135.2 509.77 135.2s-202.72 90.94-202.72 202.72 90.94 202.72 202.72 202.72z m0-343.43c77.59 0 140.72 63.12 140.72 140.72s-63.12 140.72-140.72 140.72c-77.59 0-140.72-63.13-140.72-140.72s63.13-140.72 140.72-140.72zM718.48 621.29H303.14c-84.06 0-152.45 68.39-152.45 152.45v66.78c0 17.12 13.88 31 31 31 17.12 0 31-13.88 31-31v-66.78c0-49.87 40.57-90.45 90.45-90.45h415.34c49.87 0 90.45 40.57 90.45 90.45v66.78c0 17.12 13.88 31 31 31s31-13.88 31-31v-66.78c0-84.06-68.39-152.45-152.45-152.45z" fill="#333333" p-id="2168"></path></svg>`, title: computed(() => t("home.tabbar.main")), to: { name: "main" } } ]); // 改变tabbar颜色 const getSvgIcon = (originSvg, isActive) => { let color = isActive ? '#4EADF8' : '#999999'; return originSvg.replace(/#333333/g, color); } </script> <style lang="scss" scoped> .tabbar_sp { font-weight: 550; font-size: 10px; color: #223464; } :deep(.van-tabbar) { .van-tabbar-item--active { .tabbar_icon { animation: van-tabbar-item-active 0.3s ease-in-out; } } } @keyframes van-tabbar-item-active { 20% { transform: scale(0.9); } 100% { transform: scale(1); } } </style> 修改代码 增加点击状态下的另一种svg显示 并且是渐变色
最新发布
10-11
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值