You didn't consider the gas cost for `swap` and walk in the `tree` which will bring additional `gas` consumption.
You didn't consider the gas cost for
swapand walk in thetreewhich will bring additionalgasconsumption.